package com.smart.domain;
import com.smart.dao.DBoperator;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class MonitorUserDaolmpl implements MonitorUserDao{
    @Override
    public List selectall() throws Exception {
        PreparedStatement pstmt= null;
        DBoperator dbc = null;
        String sql = "select * from stu ";
        try {
            dbc = new DBoperator();
            pstmt = dbc.getConnection().prepareStatement(sql);
            ResultSet rs = pstmt.executeQuery();
            int col = rs.getMetaData().getColumnCount();
            System.out.println("=========================");
            while (rs.next()) {
                for (int i=1; i <= col; i++) {
                    System.out.print(rs.getString(i) + "\t");
                    if ((i == 2) && (rs.getString(i).length() < 8)) {
                        System.out.print("\t");
                    }
                }
                System.out.println(" ");
            }
            System.out.println("========================");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    @Override
    public List<MonitorUser> selectid(String id) {
        List<MonitorUser> list=new ArrayList<MonitorUser>();
        String SQL="select * from stu where sdept=?";
        DBoperator stmt = null;
        stmt = new DBoperator();
        Connection coon = stmt.getConnection();
        try {
            PreparedStatement statement1 = coon.prepareStatement(SQL);
            statement1.setString(1,id);
            ResultSet resultSet = statement1.executeQuery();
            while(resultSet.next()){
                MonitorUser  user = new MonitorUser();//每一次都要重新new一个
                user.setSnumber(resultSet.getString("number"));
                user.setSname(resultSet.getString("id"));
                //user.setSsex(resultSet.getString("sex"));
                user.setGrade(resultSet.getString("grade"));
                user.setSclass(resultSet.getString("sclass"));
                list.add(user);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
}
